// pages/Index/shopActive/friendCards/addFriendCards.js
const controller=require("../../../controllers/Controller").controller
const dateTimePicker = require('../../../../utils/dateTimePicker.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime:'请选择时间',
    endTime:'请选择时间',
    dateTimeArray1: '',
    dateTime1: '',
    dateTimeArray2: '',
    dateTime2: '',
    price:"",
    type:4,
    haveCoupon:1,
    couponIdNum:"",
    name:"",
    chooseCoupons:"",
    shopImg:"",
    allNum:0,
    allPrice:0,
    cost:0,
    timeState:0,
    effectiveTime:"",
    isSelect:false
  },

  // 切换是否能加入一卡通
  changeIfAddActivity(e){
    const that = this
    this.setData({
      isSelect:false
    })
    if(!e.detail.value)return
    wx.showModal({
      content: '开启商圈按钮后,无法进行操作赠送和编辑',
      confirmText:"确定开启",
      success (res) {
        if (res.confirm) {
          that.setData({
            isSelect:true
          })
        }
      }
    })
  },

  changeinput(e){
    var name=e.currentTarget.dataset.name;
    var val=e.detail.value;
    this.setData({
        [name]:val || ''
    })
  },

  settimeState(e){
    this.setData({
      timeState:e.currentTarget.dataset.type,
    })
  },

  changeDateTime1(e) {
    var date=this.data.dateTimeArray1
    var time=e.detail.value
     this.setData({
      dateTime1: time,
      startTime:date[0][time[0]]+"-"+date[1][time[1]]+"-"+date[2][time[2]]+" "+date[3][time[3]]+":"+date[4][time[4]]
     });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr,
    });
  },
  changeDateTime2(e) {
    var date=this.data.dateTimeArray2
    var time=e.detail.value
     this.setData({
      dateTime2: time,
      endTime:date[0][time[0]]+"-"+date[1][time[1]]+"-"+date[2][time[2]]+" "+date[3][time[3]]+":"+date[4][time[4]]
     });
  },
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr,
    });
  },

  //计算卡片总价值
  computeNum(){
    let list=this.data.chooseCoupons
    let allPrice=0
    let allNum=0
    list.forEach(item=>{
      allPrice=allPrice+item.selectNum*item.price
      allNum=allNum+item.selectNum
    })
    this.setData({
      allPrice:allPrice.toFixed(2),
      allNum:allNum
    })
  },

  //获取活动名字
  getName(e){
    this.setData({
      name:e.detail.value
    })
  },

  //添加优惠券
  addCouponNum(e){
    let index=e.currentTarget.dataset.index
    let list=this.data.chooseCoupons
    list[index].selectNum=list[index].selectNum+1
    this.setData({
      chooseCoupons:list
    })
    this.computeNum()
  },

  //减少优惠券
  reduceCouponNum(e){
    let index=e.currentTarget.dataset.index
    let list=this.data.chooseCoupons
    if(list[index].selectNum>0){
      list[index].selectNum=list[index].selectNum-1
      if(list[index].selectNum<=0){
        list.splice(index,1)
      }
    }
    this.setData({
      chooseCoupons:list
    })
    this.computeNum()
  },

  //取消按钮
  cancleBtn(){
    wx.navigateBack()
  },

  //新增引流卡
  addCard(){
    let value={}
    let list=[]
    value.price=this.data.allPrice
    value.type=this.data.type
    value.haveCoupon=this.data.haveCoupon
    value.name=this.data.name
    value.shopId=wx.getStorageSync('shopId')
    value.haveGoods=0
    value.timeState=this.data.timeState;
    value.startTime=this.data.startTime=='请选择时间'?'':this.data.startTime;
    value.endTime=this.data.endTime=='请选择时间'?'':this.data.endTime;
    value.effectiveTime=this.data.effectiveTime;
    value.cost=this.data.allPrice;

    this.data.chooseCoupons.forEach(item=>{
      list.push(item.id+'-'+item.selectNum)
    })
    value.couponIdNum=list.join(",")

    if(this.data.isSelect==false){
      value.openGroup=0
    }else{
      value.openGroup=1
    }


    controller.vipsave(value).then(res=>{
      wx.showToast({
        title: '添加成功',
      })
      setTimeout(function(){
        wx.navigateBack()
      },1500)
    })
  },

  //跳转优惠券选择页面
  toSelectCoupon(){
    wx.navigateTo({
      url: '/pages/Index/shopActive/friendCards/selectCoupons',
    })
  },

  //获取引流卡详情
  vipdetails(){
    controller.vipdetails({shopId:wx.getStorageSync('shopId'),vipId:this.data.id}).then(res=>{
          if(res.data.startTime)
          res.data.startTime = res.data.startTime.replace(/-/g, '/')
          if(res.data.endTime)
          res.data.endTime = res.data.endTime.replace(/-/g, '/')

          res.data.coupon.forEach(item=>{
            item.selectNum=item.couponNum
          })

      this.setData({
        allPrice:res.data.price||"",
        type:res.data.type||"",
        haveCoupon:res.data.haveCoupon||"",
        name:res.data.name||"",
        shopId:res.data.shopId||"",
        haveGoods:0,
        timeState:res.data.timeState||"",
        startTime:res.data.startTime||"",
        endTime:res.data.endTime||"",
        effectiveTime:res.data.effectiveTime||"",
        cost:res.data.cost||"",
        chooseCoupons:res.data.coupon||""
      })
      if(res.data.openGroup==1){
        this.setData({
          isSelect:true
        })
      }else{
        this.setData({
          isSelect:false
        })
      }
      this.computeNum()
    })
  },

  //编辑会员卡内容
  setVipCard(){
    let list=[]
    let value={}
    value.id=this.data.id
    value.price=this.data.allPrice
    value.type=this.data.type
    value.haveCoupon=this.data.haveCoupon
    value.name=this.data.name
    value.shopId=wx.getStorageSync('shopId')
    value.haveGoods=0
    value.timeState=this.data.timeState;
    value.startTime=this.data.startTime=='请选择时间'?'':this.data.startTime;
    value.endTime=this.data.endTime=='请选择时间'?'':this.data.endTime;
    value.effectiveTime=this.data.effectiveTime;
    value.cost=this.data.allPrice;
    this.data.chooseCoupons.forEach(item=>{
      list.push(item.id+'-'+item.selectNum)
    })
    value.couponIdNum=list.join(",")

    controller.updateOpenVip(value).then(res=>{
      wx.showToast({
        title: '操作成功',
      })
      setTimeout(function(){
        wx.navigateBack()
      },1000)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopImg:wx.getStorageSync('shopImg')
    })
    if(options.id){
      this.setData({
        id:options.id
      })
      this.vipdetails()
    }

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj1 = dateTimePicker.dateTimePicker();
    var obj2 = dateTimePicker.dateTimePicker();
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})